<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Layout\{Column, Row, Content};
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use App\Models\Setting;

class SettingController extends Controller
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '基础设置';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('基础设置')
            ->body($this->form());
    }

        /**
     * 表单
     * @return Form
     */
    public function form($id=0)
    {
        $form = new Form(new Setting);
        $form->setTitle(' ');
        $form->setAction('setting/save');

        //获取设置信息
        $rate_euro = Setting::get('rate_euro');
        !isset($rate_euro['value']) && $rate_euro['value'] = '0';

        $form->text('rate_euro', '欧元汇率')->default($rate_euro['value']);

        //表单右上角
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
            $tools->disableList();
        });

        // $form->disableReset();
        $form->disableViewCheck();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();

        return $form;
    }

    public function setting(Request $request)
    {
        $data = $request->all();

        if ($data['rate_euro'])
        {
            Setting::set(trim($data['rate_euro']), 'rate_euro');
        }

        admin_success('保存成功');

        return redirect('/admin/setting')->with('保存成功');
    }
}
